package io.renren.modules.sys.subject.controller;

import io.renren.common.utils.R;
import io.renren.modules.sys.subject.entity.QuestionEntity;
import io.renren.modules.sys.subject.service.QuestionService;
import oracle.jdbc.proxy.annotation.Post;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.UUID;
/**
 * @author flagdied
 * @email 2481019556@qq.com
 * @date 2020-05-01 15:11:48
 */
@RestController
@RequestMapping("/file")
public class FileController {
	@Autowired
	private QuestionService questionService;
	private String fileIndex = "D:/picture/";//图片存放磁盘的路径


	{
		File file = new File(fileIndex);
		if (!file.exists()) {
			file.mkdirs();
		} else {
			System.out.println("路径文件" + fileIndex + "已经存在，将进行存储下载操作");
		}
	}

	@RequestMapping("/download/{name}")
	@RequiresPermissions("subject:question:list")
	public void downLoad(HttpServletResponse response, @PathVariable("name") String name) throws IOException {
		//String filename="1.txt";
		if (name.length() == 0) {
			return;
		}
		File file = new File(fileIndex + name);
		if (file.exists()) { //判断文件是否存在
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			// response.setContentType("application/force-download");
			response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(name, "UTF-8"));
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
			int n = in.available();
			byte[] buffer = new byte[n];
			in.read(buffer);
			in.close();
			BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
			out.write(buffer);
			out.close();
		}
	}

	@PostMapping("/upload")
	@RequiresPermissions("subject:question:save")
	public R upLoad(
			@RequestParam(name = "file", required = false) MultipartFile file,
			@RequestParam(name = "filename", required = false) String filename
	) throws IOException {
		if (file == null) {
			return null;
		}
		String[] split = filename.split("-");
		int id=Integer.valueOf(split[0]);//20
		filename = split[0] + filename.substring(filename.lastIndexOf('.'));//  20.jpg
		file.transferTo(new File(fileIndex + filename));
		QuestionEntity questionEntity = new QuestionEntity();
		questionEntity.setId(id);
		questionEntity.setImageurl(filename);
		questionService.updateImageUrl(questionEntity);
		return R.ok();
	}

	@RequestMapping("/show/{name}")
	@RequiresPermissions( "subject:question:list")
	public void show(HttpServletResponse response, @PathVariable(value = "name", required = false) String filePath) throws IOException {
		if ("".equals(filePath)) {
			return;
		}
		response.setContentType("image/jpeg");
		BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
		filePath = fileIndex + filePath;
		File file = new File(filePath);
		if (file.exists()) {
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
			int n = in.available();
			byte[] buffer = new byte[n];
			in.read(buffer);
			in.close();
			out.write(buffer);
		}
		out.close();
	}

}
